package com.beverages.commodities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beverages.commodities.common.Result;
import com.beverages.commodities.entity.Commodities;
import com.beverages.commodities.entity.Toppings;
import com.beverages.commodities.service.IToppingsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/merchant/toppings")
public class ToppingsController {
    @Resource
    private IToppingsService toppingsService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Toppings toppings) {
        toppingsService.saveOrUpdate(toppings);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delist(@PathVariable Integer id) {
        // 创建一个更新对象
        Toppings toppings = new Toppings();
        toppings.setWhetherToList(false); // 设置whether_to_list为false
        toppings.setId(id); // 设置要更新的记录的ID

        // 更新记录
        return Result.success(toppingsService.updateById(toppings));
    }

//    @GetMapping("/relist/{id}")
//    public Result relist(@PathVariable Integer id) {
//        // 创建一个更新对象
//        Toppings toppings = new Toppings();
//        toppings.setWhetherToList(true); // 设置whether_to_list为true
//        toppings.setId(id); // 设置要更新的记录的ID
//
//        // 更新记录
//        return Result.success(toppingsService.updateById(toppings));
//    }

    @PostMapping("/delist/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        // 创建更新条件
        LambdaUpdateWrapper<Toppings> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Toppings::getId, ids); // 设置要更新的ID列表
        updateWrapper.set(Toppings::getWhetherToList, false); // 将whether_to_list设置为false

        // 执行批量更新
        return Result.success(toppingsService.update(updateWrapper));
    }

    @GetMapping
    public Result findAll() {
        return Result.success(toppingsService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(toppingsService.getById(id));
    }

    @GetMapping("/page")
    public Result<IPage<Toppings>> findPage(@RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize,
                                            @RequestParam(defaultValue = "") String name,
                                            @RequestParam(defaultValue = "true") Boolean whetherOnlyShowListedToppings) {
        // 创建分页对象
        IPage<Toppings> page = new Page<>(pageNum, pageSize);

        // 创建查询条件
        QueryWrapper<Toppings> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)) {
            queryWrapper.like("name", name.trim());
        }
        if(whetherOnlyShowListedToppings){
            queryWrapper.eq("whether_to_list",1);
        }
        // 执行分页查询
        IPage<Toppings> resultPage = toppingsService.page(page, queryWrapper);

        return Result.success(resultPage);
    }

    @GetMapping("/list")
    public Result getToppingsByIds(@RequestParam("toppingsIds") List<Integer> toppingsIds){
        return Result.success(toppingsService.getToppingsByIds(toppingsIds));
    }
}
